package com.alianzamedica.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.objectsearch.sqlsearch.ObjectSearch;
import org.w3c.dom.Document;

import com.alianzamedica.businessobject.Prescription;
import com.alianzamedica.businessobject.PrescriptionDetail;
import com.alianzamedica.models.PrescriptionSellingModel;
import com.alianzamedica.tools.Enviroment;

/**
 * @author Carlos
 * 
 */
public class PrescriptionUpdater extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7247584659875010608L;

	@SuppressWarnings("unchecked")
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		try {
			String tag = request.getParameter("tagID");
			Prescription prescription = this.getPrescriptionFromTag(tag);

			Enumeration enumeration = request.getParameterNames();
			PrescriptionSellingModel sellModel = new PrescriptionSellingModel();
			ArrayList<PrescriptionDetail> details = new ArrayList<PrescriptionDetail>();
			while (enumeration.hasMoreElements()) {
				String parameter = (String) enumeration.nextElement();

				if (parameter.startsWith("line_")) {
					String num = parameter.replaceAll("line_", "");
					Integer number = Integer.valueOf(num);
					String barCode = request.getParameter(parameter);
					String q = request.getParameter("nb_" + number);
					Float quantitySold = Float.valueOf(q);
					PrescriptionDetail detail = sellModel.getDetailFromBarCode(
							prescription, barCode);
					detail.setQuantitySold(quantitySold);
					details.add(detail);
				}

			}
			sellModel.updateSellDetails(details);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (Exception e) {
			}
		}

	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.processRequest(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.processRequest(req, resp);
	}

	@SuppressWarnings("unchecked")
	private Prescription getPrescriptionFromTag(String tag) throws Exception {
		Enviroment env = Enviroment.getInstance();
		Document doc = env.getDocument();
		ObjectSearch search = new ObjectSearch(doc,
				"com.alianzamedica.connection.ConnectionImpl");
		Prescription p = new Prescription();
		p.setTag(tag);
		Iterator<Prescription> iterator = search.searchObjects(p).iterator();
		Prescription prescription = null;
		while (iterator.hasNext()) {
			prescription = (Prescription) iterator.next();
		}
		return prescription;
	}

}
